var roadStatus = require('./roadStatus');

$(document).ready(function(){
	var vectorSource = new ol.source.Vector({
        format: new ol.format.GeoJSON(),
        url: function(extent, resolution, projection) {
         /* return 'http://localhost:8081/geoserver/lee/wfs?service=WFS&' +
              'version=1.1.0&request=GetFeature&typename=lee:%E5%B8%82%E5%8C%BA%E9%81%93%E8%B7%AF_polyline&' +
              'outputFormat=application/json&' +
              'bbox=' + extent.join(',') + ',EPSG:3857';*/
              return  'http://localhost:8081/geoserver/lee/wfs?service=WFS&version=1.0.0&request=GetFeature&typeName=lee:%E5%B8%82%E5%8C%BA%E9%81%93%E8%B7%AF_polyline&outputFormat=json&' +
              'bbox=' + extent.join(',')+ ',EPSG:3857';
        },
        strategy: ol.loadingstrategy.tile(ol.tilegrid.createXYZ({
          maxZoom: 19
        }))
      });


	//style
	var styles = {
		'通畅': new ol.style.Style({
          stroke: new ol.style.Stroke({
            color: 'rgba(102, 204, 0, 0.5)',
            width: 4
          })
        }),
       '正常': new ol.style.Style({
          stroke: new ol.style.Stroke({
            color: 'rgba(102,204,51, 0.5)',
            width: 4
          })
        }),
        '拥堵': new ol.style.Style({
          stroke: new ol.style.Stroke({
            color: 'rgba(255, 152, 0, 0.6)',
            width: 4
          })
        }),
        '无法通行': new ol.style.Style({
          stroke: new ol.style.Stroke({
            color: 'rgba(255, 51, 0, 0.8)',
            width: 4
          })
        })
	}

//http://localhost:8081/geoserver/lee/wfs?service=WFS&version=1.0.0&request=GetFeature&typeName=lee:%E5%B8%82%E5%8C%BA%E9%81%93%E8%B7%AF_polyline&maxFeatures=50&outputFormat=json
      var vector = new ol.layer.Vector({
        source: vectorSource,
        style:  function(feature, resolution){
        	/*roadStatus.update([feature.xa]);
        	return styles[roadStatus.getByRoad(feature.xa)];*/
        	 var s = roadStatus.getRandomState();
			return styles[s];
        }
      });

      var select = new ol.interaction.Select({
        wrapX: false
      });

      var modify = new ol.interaction.Modify({
        features: select.getFeatures()
      });

      var raster = new ol.layer.Tile({
        source: new ol.source.MapQuest({layer: 'osm'})
      })



      var map = new ol.Map({
      	 //interactions: ol.interaction.defaults().extend([select, modify]),
        layers: [raster, vector],
        target: document.getElementById('map'),
        view: new ol.View({
          center: [13407148, 3540099],
          maxZoom: 19,
          zoom: 16
        })
      });

      //点击事件
    map.addInteraction(select);
	select.on('select', function(e) {

		allFeatures = vector.getSource().getFeatures();
		var ids = [];
		for(key in allFeatures){
			ids.push(allFeatures[key].xa);
		}
		roadStatus.update(ids);
		    
		   console.log(e.selected[0].xa);
		   var id = e.selected[0].xa;
		   alert(roadStatus.getByRoad(id));
	});


	var allFeatures ;
	//获取所有feature
	setTimeout(function(){
		console.log(vector.getSource().getFeatures());
		allFeatures = vector.getSource().getFeatures();
		var ids = [];
		for(key in allFeatures){
			ids.push(allFeatures[key].xa);
		}
		roadStatus.update(ids);
	},2000)
	 
})

 